﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sinacor.UI.Bovespa.Monitoracao.Presenter;
using Sinacor.Infra.UI.Controls.SmartClient;
using System.Windows.Media.Animation;
using Sinacor.UI.Bovespa.Monitoracao.Model;
using Sinacor.Infra.UI.Messaging;
using Sinacor.Infra.UI.Messaging.SmartClient;
using Sinacor.Infra.UI.Messaging.Internal;

namespace Sinacor.UI.Bovespa.Monitoracao.View.SmartClient
{
    /// <summary>
    /// Interaction logic for STMMonitor.xaml
    /// </summary>
    public partial class STMMonitor : SinacorWindow, ISTMMonitor
    {
        Presenter.STMMonitor _presenter;
        private bool _eventoOcorreu = false;
        private bool _hftFora = false;
        public STMMonitor()
        {
            InitializeComponent();
            this.AfterInitialize += new RoutedEventHandler(STMMonitor_AfterInitialize);
            this.Closing += new System.ComponentModel.CancelEventHandler(STMMonitor_Closing);
            this.Closed += new EventHandler(STMMonitor_Closed);
            this.LedRoteadorMensagensFilaGDadosNOK.Visibility = Visibility.Hidden;
            this.LedRoteadorMensagensFilaGDadosOK.Visibility = Visibility.Hidden;
            this.LedRoteadorMensagensFilaGDadosInativo.Visibility = Visibility.Hidden;
        }

        void STMMonitor_Closed(object sender, EventArgs e)
        {
            _presenter.DisposeHealthMonitorClient();
        }

        void STMMonitor_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _presenter.DisposeHealthMonitorClient();
        }

        void STMMonitor_AfterInitialize(object sender, RoutedEventArgs e)
        {
            _presenter = new Sinacor.UI.Bovespa.Monitoracao.Presenter.STMMonitor(this);
            _presenter.InitializeMonitor();
            this.DataContext = _presenter;

        }

        delegate void dRefreshMonitor();

        public void RefreshMonitor()
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dRefreshMonitor d = new dRefreshMonitor(RefreshMonitor);
                this.Dispatcher.Invoke(d);
            }
            else
            {
                RefreshReceptorDetail();
                StateControlReceptorLeds();
                RefreshRouterDetail();
                StateControlRouterLeds();
                RefreshDumperDetail();
                StateControlDumperLeds();
                RefreshOrderExecutorDetail();
                StateControlOrderExecutorLeds();
                PopulateHFTServers();
                RefreshHFTProcessDetail();
            }
        }

        delegate void dDisableHFTMonitor();

        public void DisableHFTMonitor()
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dDisableHFTMonitor d = new dDisableHFTMonitor(DisableHFTMonitor);
                this.Dispatcher.Invoke(d);
            }
            else
            {
                if (!_hftFora)
                {
                    this.HFTProcess01.Visibility = Visibility.Hidden;
                    this.AlertaHFT01OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT01NOK.Visibility = Visibility.Hidden;
                    this.HFTProcess02.Visibility = Visibility.Hidden;
                    this.AlertaHFT02OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT02NOK.Visibility = Visibility.Hidden;
                    this.HFTProcess03.Visibility = Visibility.Hidden;
                    this.AlertaHFT03OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT03NOK.Visibility = Visibility.Hidden;
                    this.HFTProcess04.Visibility = Visibility.Hidden;
                    this.AlertaHFT04OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT04NOK.Visibility = Visibility.Hidden;
                    this.HFTProcess05.Visibility = Visibility.Hidden;
                    this.AlertaHFT05OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT05NOK.Visibility = Visibility.Hidden;
                    this.HFTProcess06.Visibility = Visibility.Hidden;
                    this.AlertaHFT06OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT06NOK.Visibility = Visibility.Hidden;
                    this.HFTProcess07.Visibility = Visibility.Hidden;
                    this.AlertaHFT07OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT07NOK.Visibility = Visibility.Hidden;
                    this.HFTProcess08.Visibility = Visibility.Hidden;
                    this.AlertaHFT08OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT08NOK.Visibility = Visibility.Hidden;
                }
            }
        }

        private void PopulateHFTServers()
        {
            if (_presenter.GlobalHftStatus)
            {
                this.AlertaHFTServersOK.Visibility = Visibility.Visible;
                this.AlertaHFTServersNOK.Visibility = Visibility.Hidden;
                this.AlertaHFTServersDown.Visibility = Visibility.Hidden;
            }
            else
            {
                this.AlertaHFTServersOK.Visibility = Visibility.Hidden;
                this.AlertaHFTServersNOK.Visibility = Visibility.Hidden;
                this.AlertaHFTServersDown.Visibility = Visibility.Visible;
            }

            int NumberOfHFTProcess = _presenter.HFTProcessList.Count;
            if (NumberOfHFTProcess >= 1 && _presenter.HFTProcessList[0] != null && string.IsNullOrEmpty(_presenter.HFTProcessList[0].ErrorData.ErrorCode))
            {
                this.HFTProcess01.Tag = (object)_presenter.HFTProcessList[0];
                if (_presenter.CheckHftProcessOk(_presenter.HFTProcessList[0]))
                {
                    this.HFTProcess01.Visibility = Visibility.Visible;
                    this.AlertaHFT01OK.Visibility = Visibility.Visible;
                    this.AlertaHFT01NOK.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.HFTProcess01.Visibility = Visibility.Visible;
                    this.AlertaHFT01OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT01NOK.Visibility = Visibility.Visible;
                }
            }
            else
            {
                if (!_hftFora)
                {
                    this.HFTProcess01.Visibility = Visibility.Hidden;
                    this.AlertaHFT01OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT01NOK.Visibility = Visibility.Hidden;
                }
            }

            if (NumberOfHFTProcess >= 2 && _presenter.HFTProcessList[1] != null)
            {
                this.HFTProcess02.Tag = (object)_presenter.HFTProcessList[1];
                if (_presenter.CheckHftProcessOk(_presenter.HFTProcessList[1]))
                {
                    this.HFTProcess02.Visibility = Visibility.Visible;
                    this.AlertaHFT02OK.Visibility = Visibility.Visible;
                    this.AlertaHFT02NOK.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.HFTProcess02.Visibility = Visibility.Visible;
                    this.AlertaHFT02OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT02NOK.Visibility = Visibility.Visible;
                }
            }
            else
            {
                this.HFTProcess02.Visibility = Visibility.Hidden;
                this.AlertaHFT02OK.Visibility = Visibility.Hidden;
                this.AlertaHFT02NOK.Visibility = Visibility.Hidden;
            }

            if (NumberOfHFTProcess >= 3 && _presenter.HFTProcessList[2] != null)
            {
                this.HFTProcess03.Tag = (object)_presenter.HFTProcessList[2];
                if (_presenter.CheckHftProcessOk(_presenter.HFTProcessList[2]))
                {
                    this.HFTProcess03.Visibility = Visibility.Visible;
                    this.AlertaHFT03OK.Visibility = Visibility.Visible;
                    this.AlertaHFT03NOK.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.HFTProcess03.Visibility = Visibility.Visible;
                    this.AlertaHFT03OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT03NOK.Visibility = Visibility.Visible;
                }
            }
            else
            {
                this.HFTProcess03.Visibility = Visibility.Hidden;
                this.AlertaHFT03OK.Visibility = Visibility.Hidden;
                this.AlertaHFT03NOK.Visibility = Visibility.Hidden;
            }


            if (NumberOfHFTProcess >= 4 && _presenter.HFTProcessList[3] != null)
            {
                this.HFTProcess04.Tag = (object)_presenter.HFTProcessList[3];
                if (_presenter.CheckHftProcessOk(_presenter.HFTProcessList[3]))
                {
                    this.HFTProcess04.Visibility = Visibility.Visible;
                    this.AlertaHFT04OK.Visibility = Visibility.Visible;
                    this.AlertaHFT04NOK.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.HFTProcess04.Visibility = Visibility.Visible;
                    this.AlertaHFT04OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT04NOK.Visibility = Visibility.Visible;
                }
            }
            else
            {
                this.HFTProcess04.Visibility = Visibility.Hidden;
                this.AlertaHFT04OK.Visibility = Visibility.Hidden;
                this.AlertaHFT04NOK.Visibility = Visibility.Hidden;
            }

            if (NumberOfHFTProcess >= 5 && _presenter.HFTProcessList[4] != null)
            {
                this.HFTProcess05.Tag = (object)_presenter.HFTProcessList[4];
                if (_presenter.CheckHftProcessOk(_presenter.HFTProcessList[4]))
                {
                    this.HFTProcess05.Visibility = Visibility.Visible;
                    this.AlertaHFT05OK.Visibility = Visibility.Visible;
                    this.AlertaHFT05NOK.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.HFTProcess05.Visibility = Visibility.Visible;
                    this.AlertaHFT05OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT05NOK.Visibility = Visibility.Visible;
                }
            }
            else
            {
                this.HFTProcess05.Visibility = Visibility.Hidden;
                this.AlertaHFT05OK.Visibility = Visibility.Hidden;
                this.AlertaHFT05NOK.Visibility = Visibility.Hidden;
            }

            if (NumberOfHFTProcess >= 6 && _presenter.HFTProcessList[5] != null)
            {
                this.HFTProcess06.Tag = (object)_presenter.HFTProcessList[5];
                if (_presenter.CheckHftProcessOk(_presenter.HFTProcessList[5]))
                {
                    this.HFTProcess06.Visibility = Visibility.Visible;
                    this.AlertaHFT06OK.Visibility = Visibility.Visible;
                    this.AlertaHFT06NOK.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.HFTProcess06.Visibility = Visibility.Visible;
                    this.AlertaHFT06OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT06NOK.Visibility = Visibility.Visible;
                }
            }
            else
            {
                this.HFTProcess06.Visibility = Visibility.Hidden;
                this.AlertaHFT06OK.Visibility = Visibility.Hidden;
                this.AlertaHFT06NOK.Visibility = Visibility.Hidden;
            }

            if (NumberOfHFTProcess >= 7 && _presenter.HFTProcessList[6] != null)
            {
                this.HFTProcess07.Tag = (object)_presenter.HFTProcessList[6];
                if (_presenter.CheckHftProcessOk(_presenter.HFTProcessList[6]))
                {
                    this.HFTProcess07.Visibility = Visibility.Visible;
                    this.AlertaHFT07OK.Visibility = Visibility.Visible;
                    this.AlertaHFT07NOK.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.HFTProcess07.Visibility = Visibility.Visible;
                    this.AlertaHFT07OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT07NOK.Visibility = Visibility.Visible;
                }
            }
            else
            {
                this.HFTProcess07.Visibility = Visibility.Hidden;
                this.AlertaHFT07OK.Visibility = Visibility.Hidden;
                this.AlertaHFT07NOK.Visibility = Visibility.Hidden;
            }

            if (NumberOfHFTProcess >= 8 && _presenter.HFTProcessList[7] != null)
            {
                this.HFTProcess08.Tag = (object)_presenter.HFTProcessList[7];
                if (_presenter.CheckHftProcessOk(_presenter.HFTProcessList[7]))
                {
                    this.HFTProcess08.Visibility = Visibility.Visible;
                    this.AlertaHFT08OK.Visibility = Visibility.Visible;
                    this.AlertaHFT08NOK.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.HFTProcess08.Visibility = Visibility.Visible;
                    this.AlertaHFT08OK.Visibility = Visibility.Hidden;
                    this.AlertaHFT08NOK.Visibility = Visibility.Visible;
                }
            }
            else
            {
                this.HFTProcess08.Visibility = Visibility.Hidden;
                this.AlertaHFT08OK.Visibility = Visibility.Hidden;
                this.AlertaHFT08NOK.Visibility = Visibility.Hidden;
            }
        }

        private void RefreshHFTProcessDetail()
        {
            this.LblProcessoHFTFilaFilaAcessivelStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTFilaFilaAcessivelLimit.UpdateTarget(Label.ContentProperty);
            this.LblProcessosHFTFilaMensagensStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessosHFTFilaMensagensLimit.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTFilaTamanhoFilaStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTFilaTamanhoFilaLimit.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoHorarioStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoTotalMensagensStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoMsgsProcessadasStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoTempoAguardandoStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoTempoAguardandoLimit.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoTentativasMSMQStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoTempoMedioBancoStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoTempoMedioBancoLimit.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoConexoesSucessoStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoConexoesErroStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoPrivateMemoryStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoPrivateMemoryLimit.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoHandleCountStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoHandleCountLimit.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoThreadCountStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoThreadCountLimit.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoPrivilegedTimeStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoInicioProcessoStats.UpdateTarget(Label.ContentProperty);
            this.LblProcessoHFTProcessoProcessUpTimeStats.UpdateTarget(Label.ContentProperty);
            this.HFTIdentification.UpdateTarget(Label.ContentProperty);
            this.FillToolTipClient();
            this.FillToolTipPorts();
        }

        private void RefreshRouterDetail()
        {
            this.LblRoteadorMensagensTentConexao.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensTraduzidasporSegundo.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensMsgsRecebidas.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensRoteadasporSeg.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensFilaInterna02.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensFilaInterna03.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensRoteadasFluxoNormal.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensRoteadasparaHFT.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensOtimizadas.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensBlocadasBDadosporVez.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensMediaInsercBDados.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensFilaBDados.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensConexaoSucessoBDados.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensConexaoErroBDados.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensRoteadasTotal.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorPrivateMemory.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorHandlerCount.UpdateTarget(Label.ContentProperty);
            this.LblRoteadorMensagensThreadCount.UpdateTarget(Label.ContentProperty);
            this.LblFilaRoteadorMsgRepresadas.UpdateTarget(Label.ContentProperty);
            this.LblFilaRoteadorTamanhoFila.UpdateTarget(Label.ContentProperty);

            this.ToolTipInicioProcessoRoteador.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.InicioProcesso").ToString(), _presenter.entityMonitor.RouterMonitor.InitializedDate.ToShortDateString());
            this.ToolTipIPMaquinaRoteador.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.IPMaquina").ToString(), _presenter.entityMonitor.RouterMonitor.MachineIPAddress);
            this.ToolTipNomeMaquinaRoteador.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.NomeMaquinaFluxo").ToString(), _presenter.entityMonitor.RouterMonitor.MachineName);
            this.ToolTipPrivilegedProcessorTimeRoteador.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.PrivilegedProcessorTime").ToString(), _presenter.entityMonitor.RouterMonitor.PrivilegedProcessorTime);
            this.ToolTipProcessUpTimeRoteador.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.ProcessUpTime").ToString(), _presenter.entityMonitor.RouterMonitor.UpTime.ToString());
        }

        private void StateControlRouterLeds()
        {
            if (_presenter.RouterDataBaseQueueOk)
            {
                this.LedRoteadorMensagensFilaGDadosNOK.Visibility = Visibility.Hidden;
                this.LedRoteadorMensagensFilaGDadosOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedRoteadorMensagensFilaGDadosNOK.Visibility = Visibility.Hidden;
                this.LedRoteadorMensagensFilaGDadosOK.Visibility = Visibility.Hidden;
            }

            if (_presenter.RouterHandleCountOk)
            {
                this.LedRoteadorHandleCountNOK.Visibility = Visibility.Hidden;
                this.LedRoteadorHandleCountOK.Visibility = Visibility.Visible;
            }
            else
            {
                this.LedRoteadorHandleCountNOK.Visibility = Visibility.Visible;
                this.LedRoteadorHandleCountOK.Visibility = Visibility.Hidden;
            }

            if (_presenter.RouterInsertDataBaseMessagesOk)
            {
                this.LedRoteadorMensagensMInsBDadosNOK.Visibility = Visibility.Hidden;
                this.LedRoteadorMensagensMInsBDadosOK.Visibility = Visibility.Visible;
            }
            else
            {
                this.LedRoteadorMensagensMInsBDadosNOK.Visibility = Visibility.Visible;
                this.LedRoteadorMensagensMInsBDadosOK.Visibility = Visibility.Hidden;
            }

            if (_presenter.RouterInternalQueue1Ok)
            {
                this.LedRoteadorMensagensFilaInterna2NOK.Visibility = Visibility.Hidden;
                this.LedRoteadorMensagensFilaInterna2OK.Visibility = Visibility.Visible;
            }
            else
            {
                this.LedRoteadorMensagensFilaInterna2NOK.Visibility = Visibility.Visible;
                this.LedRoteadorMensagensFilaInterna2OK.Visibility = Visibility.Hidden;
            }

            if (_presenter.RouterInternalQueue2Ok)
            {
                this.LedRoteadorMensagensFilaInterna3NOK.Visibility = Visibility.Hidden;
                this.LedRoteadorFilaInterna3OK.Visibility = Visibility.Visible;
            }
            else
            {
                this.LedRoteadorMensagensFilaInterna3NOK.Visibility = Visibility.Visible;
                this.LedRoteadorFilaInterna3OK.Visibility = Visibility.Hidden;
            }

            if (_presenter.RouterMessagesOk)
            {
                this.LedRoteadorMensagensMsgRotOK.Visibility = Visibility.Visible;
                this.LedRoteadorMensagensMsgRotNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedRoteadorMensagensMsgRotOK.Visibility = Visibility.Hidden;
                this.LedRoteadorMensagensMsgRotNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.RouterPrivateMemoryOk)
            {
                this.LedRoteadorPrivateMemoryNOK.Visibility = Visibility.Hidden;
                this.LedRoteadorPrivateMemoryOK.Visibility = Visibility.Visible;
            }
            else
            {
                this.LedRoteadorPrivateMemoryNOK.Visibility = Visibility.Visible;
                this.LedRoteadorPrivateMemoryOK.Visibility = Visibility.Hidden;
            }

            if (_presenter.RouterThreadCountOk)
            {
                this.LedRoteadorThreadCountNOK.Visibility = Visibility.Hidden;
                this.LedRoteadorThreadCountOK.Visibility = Visibility.Visible;
            }
            else
            {
                this.LedRoteadorThreadCountNOK.Visibility = Visibility.Visible;
                this.LedRoteadorThreadCountOK.Visibility = Visibility.Hidden;
            }
            if (_presenter.RouterMessagesOk)
            {
                this.LedFilaRoteadorMsgRepresadasOK.Visibility = Visibility.Visible;
                this.LedFilaRoteadorMsgRepresadasNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedFilaRoteadorMsgRepresadasOK.Visibility = Visibility.Hidden;
                this.LedFilaRoteadorMsgRepresadasNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.RouterQueueOk)
            {
                this.LedFilaRoteadorFilaAcessivelOK.Visibility = Visibility.Visible;
                this.LedFilaRoteadorFilaAcessivelNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedFilaRoteadorFilaAcessivelOK.Visibility = Visibility.Hidden;
                this.LedFilaRoteadorFilaAcessivelNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.RouterQueueSizeOk)
            {
                this.LedFilaRoteadorTamFilaOK.Visibility = Visibility.Visible;
                this.LedFilaRoteadorTamFilaNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedFilaRoteadorTamFilaOK.Visibility = Visibility.Hidden;
                this.LedFilaRoteadorTamFilaNOK.Visibility = Visibility.Visible;
            }
        }

        private void RefreshReceptorDetail()
        {
            this.LblReceptorMensagensMsgRecebporSeg.UpdateTarget(Label.ContentProperty);
            this.LblReceptorMensagensHoraUltimaRecebida.UpdateTarget(Label.ContentProperty);
            this.LblReceptorMensagensTotalRecebidas.UpdateTarget(Label.ContentProperty);
            this.LblReceptorMensagensMsgID.UpdateTarget(Label.ContentProperty);
            this.LblReceptorPrivateMemory.UpdateTarget(Label.ContentProperty);
            this.LblReceptorHandleCount.UpdateTarget(Label.ContentProperty);
            this.LblReceptorThreadCount.UpdateTarget(Label.ContentProperty);
            this.ToolTipErros.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.EventosErroSTM").ToString(), _presenter.entityMonitor.ReceptorMonitor.ErrorEventsQuantity);
            this.LblReceptorSTMReconexoes.UpdateTarget(Label.ContentProperty);
            this.ToolTipTentativaErros.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.NumeroTentativasErro").ToString(), _presenter.entityMonitor.ReceptorMonitor.UnsuccessfulConnectionsQuantity);
            this.LblAckResposta.UpdateTarget(Label.ContentProperty);
            this.LblReceptorFilasTentativasRoteador.UpdateTarget(Label.ContentProperty);
            this.LblReceptorFilasTentativasDumper.UpdateTarget(Label.ContentProperty);
            this.LblReceptorFilasFilaInterna01.UpdateTarget(Label.ContentProperty);
            this.LblTempoMedioMensagem.UpdateTarget(Label.ContentProperty);
            this.ToolTipInicioProcessoReceptor.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.InicioProcesso").ToString(), _presenter.entityMonitor.ReceptorMonitor.InitializedDate.ToShortDateString());
            this.ToolTipIPMaquinaReceptor.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.IPMaquina").ToString(), _presenter.entityMonitor.ReceptorMonitor.MachineIPAddress);
            this.ToolTipNomeMaquinaReceptor.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.NomeMaquinaFluxo").ToString(), _presenter.entityMonitor.ReceptorMonitor.MachineName);
            this.ToolTipPrivilegedProcessorTimeReceptor.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.PrivilegedProcessorTime").ToString(), _presenter.entityMonitor.ReceptorMonitor.PrivilegedProcessorTime);
            this.ToolTipProcessUpTimeReceptor.Text = String.Format(FindResource("Bovespa.Monitor.Tooltip.ProcessUpTime").ToString(), _presenter.entityMonitor.ReceptorMonitor.UpTime.ToString());

        }

        private void StateControlReceptorLeds()
        {
            if (_presenter.ReceptorAverageTimeToSendAckOk)
            {
                this.LedSTMTMedioOK.Visibility = Visibility.Visible;
                this.LedSTMTMedioNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedSTMTMedioOK.Visibility = Visibility.Hidden;
                this.LedSTMTMedioNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.ReceptorHandleCountOk)
            {
                this.LedReceptorHandleCountOK.Visibility = Visibility.Visible;
                this.LedReceptorHandleCountNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedReceptorHandleCountOK.Visibility = Visibility.Hidden;
                this.LedReceptorHandleCountNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.ReceptorInternalQueuedOk)
            {
                this.LedReceptorFilasLimiteOK.Visibility = Visibility.Visible;
                this.LedReceptorFilasLimiteNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedReceptorFilasLimiteOK.Visibility = Visibility.Hidden;
                this.LedReceptorFilasLimiteNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.ReceptorPrivateMemoryOk)
            {
                this.LedReceptorPrivateMemoryOK.Visibility = Visibility.Visible;
                this.LedReceptorPrivateMemoryNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedReceptorPrivateMemoryOK.Visibility = Visibility.Hidden;
                this.LedReceptorPrivateMemoryNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.ReceptorThreadCountOk)
            {
                this.LedReceptorThreadCountOK.Visibility = Visibility.Visible;
                this.LedReceptorThreadCountNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedReceptorThreadCountOK.Visibility = Visibility.Hidden;
                this.LedReceptorThreadCountNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.ReceptorTotalInternalQueuedMessagesOk)
            {
                this.LedReceptorFilasFilaInternaOK.Visibility = Visibility.Visible;
                this.LedReceptorFilasFilaInternaNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedReceptorFilasFilaInternaOK.Visibility = Visibility.Hidden;
                this.LedReceptorFilasFilaInternaNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.STMConectionReceptorOk)
            {
                this.LedSTMConexaoOK.Visibility = Visibility.Visible;
                this.LedSTMConexaoNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedSTMConexaoOK.Visibility = Visibility.Hidden;
                this.LedSTMConexaoNOK.Visibility = Visibility.Visible;
            }
        }

        private void RefreshDumperDetail()
        {
            this.LblDumperArquivoDump.UpdateTarget(Label.ContentProperty);
            this.LblDumperArquivoDump.UpdateTarget(Label.ToolTipProperty);
            this.LblDumperMsgporSeg.UpdateTarget(Label.ContentProperty);
            this.LblDumperIPMaquina.UpdateTarget(Label.ContentProperty);
            this.LblDumperNomeMaquina.UpdateTarget(Label.ContentProperty);
            this.LblDumperHandleCount.UpdateTarget(Label.ContentProperty);
            this.LblFilaDumperMsgsRepresadas.UpdateTarget(Label.ContentProperty);
            this.LblFilaDumperTamanhoFila.UpdateTarget(Label.ContentProperty);
        }

        private void StateControlDumperLeds()
        {
            if (_presenter.DumperMessageOk)
            {
                this.LedFilaDumperMsgRepresadasOK.Visibility = Visibility.Visible;
                this.LedFilaDumperMsgRepresadasNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedFilaDumperMsgRepresadasOK.Visibility = Visibility.Hidden;
                this.LedFilaDumperMsgRepresadasNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.DumperQueueOk)
            {
                this.LedFilaDumperFilaAcessivelOK.Visibility = Visibility.Visible;
                this.LedFilaDumperFilaAcessivelNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedFilaDumperFilaAcessivelOK.Visibility = Visibility.Hidden;
                this.LedFilaDumperFilaAcessivelNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.DumperQueueSizeOk)
            {
                this.LedFilaDumperTamFilaOK.Visibility = Visibility.Visible;
                this.LedFilaDumperTamFilaNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedFilaDumperTamFilaOK.Visibility = Visibility.Hidden;
                this.LedFilaDumperTamFilaNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.DumperHandleCountOk)
            {
                this.LedDumperHandleCountOK.Visibility = Visibility.Visible;
                this.LedDumperHandleCountNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedDumperHandleCountOK.Visibility = Visibility.Hidden;
                this.LedDumperHandleCountNOK.Visibility = Visibility.Visible;
            }
          
        }

        private void RefreshOrderExecutorDetail()
        {
            this.LblTempoExecucaoProcedureOrdens.UpdateTarget(Label.ContentProperty);
            this.LblProximoHorarioOrdens.UpdateTarget(Label.ContentProperty);
            this.LblTempoExecucaoProcedureCasamento.UpdateTarget(Label.ContentProperty);
            this.LblProximoHorarioCasamento.UpdateTarget(Label.ContentProperty);
            this.LblTempoExecucaoProcedureNegocio.UpdateTarget(Label.ContentProperty);
            this.LblProximoHorarioNegocio.UpdateTarget(Label.ContentProperty);
            this.LblTempoExecucaoProcedureFinanceiro.UpdateTarget(Label.ContentProperty);
            this.LblProximoHorarioFinanceiro.UpdateTarget(Label.ContentProperty);
            this.LblExcessoes.UpdateTarget(Label.ContentProperty);
            this.LblExcessoes.Content = (_presenter.entityMonitor.BusinessExecutor.NumberOfExceptionsDataBase + _presenter.entityMonitor.FinancialExecutor.NumberOfExceptionsDataBase + _presenter.entityMonitor.MatchExecutor.NumberOfExceptionsDataBase + _presenter.entityMonitor.OrderExecutor.NumberOfExceptionsDataBase).ToString();
        }

        private void StateControlOrderExecutorLeds()
        {
            if (_presenter.OrderExecutorProcedureTimeOk)
            {
                this.LedExecutorOrdensTempoProcOK.Visibility = Visibility.Visible;
                this.LedExecutorOrdensTempoProcNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedExecutorOrdensTempoProcOK.Visibility = Visibility.Hidden;
                this.LedExecutorOrdensTempoProcNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.OrderFinancialExecutorProcedureTimeOk)
            {
                this.LedExecutorFinanceiroTempoProcOK.Visibility = Visibility.Visible;
                this.LedExecutorFinanceiroTempoProcNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedExecutorFinanceiroTempoProcOK.Visibility = Visibility.Hidden;
                this.LedExecutorFinanceiroTempoProcNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.OrderBusinessExecutorProcedureTimeOk)
            {
                this.LedExecutorNegocioTempoProcOK.Visibility = Visibility.Visible;
                this.LedExecutorNegocioTempoProcNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedExecutorNegocioTempoProcOK.Visibility = Visibility.Hidden;
                this.LedExecutorNegocioTempoProcNOK.Visibility = Visibility.Visible;
            }

            if (_presenter.OrderMatchExecutorProcedureTimeOk)
            {
                this.LedExecutorCasamentoTempoProcOK.Visibility = Visibility.Visible;
                this.LedExecutorCasamentoTempoProcNOK.Visibility = Visibility.Hidden;
            }
            else
            {
                this.LedExecutorCasamentoTempoProcOK.Visibility = Visibility.Hidden;
                this.LedExecutorCasamentoTempoProcNOK.Visibility = Visibility.Visible;
            }

        }

        private void HFTProcess01_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_presenter.entityMonitor.HFTMonitors[0] != null)
            {
                _presenter.ItemHFTProcessMonitor = _presenter.entityMonitor.HFTMonitors[0];
                _presenter.HftServerNumber = 0;
                RefreshHFTProcessDetail();
            }
        }

        private void HFTProcess02_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_presenter.entityMonitor.HFTMonitors[1] != null)
            {
                _presenter.ItemHFTProcessMonitor = _presenter.entityMonitor.HFTMonitors[1];
                _presenter.HftServerNumber = 1;
                RefreshHFTProcessDetail();
            }
        }

        private void HFTProcess03_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_presenter.entityMonitor.HFTMonitors[2] != null)
            {
                _presenter.ItemHFTProcessMonitor = _presenter.entityMonitor.HFTMonitors[2];
                _presenter.HftServerNumber = 2;
                RefreshHFTProcessDetail();
            }
        }

        private void HFTProcess04_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_presenter.entityMonitor.HFTMonitors[3] != null)
            {
                _presenter.ItemHFTProcessMonitor = _presenter.entityMonitor.HFTMonitors[3];
                _presenter.HftServerNumber = 3;
                RefreshHFTProcessDetail();
            }
        }

        private void HFTProcess05_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_presenter.entityMonitor.HFTMonitors[4] != null)
            {
                _presenter.ItemHFTProcessMonitor = _presenter.entityMonitor.HFTMonitors[4];
                _presenter.HftServerNumber = 4;
                RefreshHFTProcessDetail();
            }
        }

        private void HFTProcess06_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_presenter.entityMonitor.HFTMonitors[5] != null)
            {
                _presenter.ItemHFTProcessMonitor = _presenter.entityMonitor.HFTMonitors[5];
                _presenter.HftServerNumber = 5;
                RefreshHFTProcessDetail();
            }
        }

        private void HFTProcess07_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_presenter.entityMonitor.HFTMonitors[6] != null)
            {
                _presenter.ItemHFTProcessMonitor = _presenter.entityMonitor.HFTMonitors[6];
                _presenter.HftServerNumber = 6;
                RefreshHFTProcessDetail();
            }
        }

        private void HFTProcess08_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_presenter.entityMonitor.HFTMonitors[7] != null)
            {
                _presenter.ItemHFTProcessMonitor = _presenter.entityMonitor.HFTMonitors[7];
                _presenter.HftServerNumber = 7;
                RefreshHFTProcessDetail();
            }
        }

        public void EnableScreen()
        {
            SetEnable(this, true);
        }

        public void DisableScreen()
        {
            SetEnable(this, false);
            SetDumperState(this, false);
            SetHFTProcessState(this, false);
            SetOrderExecutorState(this, false);
            SetReceptorState(this, false);
            SetRouterState(this, false);
        }

        public void SetRouterState(bool active)
        {
            this.SetRouterState(this, active);
        }

        public void SetReceptorState(bool active)
        {
            this.SetReceptorState(this, active);
        }

        public void SetDumperState(bool active)
        {
            this.SetDumperState(this, active);
        }

        public void SetOrderExecutorState(bool active)
        {
            this.SetOrderExecutorState(this, active);
        }

        public void SetHFTProcessState(bool active)
        {
            this.SetHFTProcessState(this, active);
        }

        private delegate void dSetEnable(STMMonitor form, bool value);

        private delegate void dSetError(STMMonitor form);

        private void SetEnable(STMMonitor form, bool value)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetEnable d = new dSetEnable(SetEnable);
                this.Dispatcher.Invoke(d, new object[] { form, value });
            }
            else
            {
                form.Geral.IsEnabled = value;
            }
        }

        private void SetRouterState(STMMonitor form, bool active)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetEnable d = new dSetEnable(SetRouterState);
                this.Dispatcher.Invoke(d, new object[] { form, active });
            }
            else
            {
                form.Roteador.IsEnabled = active;
                form.Fila_Roteador.IsEnabled = active;
                if (active)
                {
                    form.LedRoteadorHandleCountInativo.Visibility = Visibility.Hidden;
                    form.LedRoteadorMensagensFilaGDadosInativo.Visibility = Visibility.Hidden;
                    form.LedRoteadorMensagensFilaInterna2Inativo.Visibility = Visibility.Hidden;
                    form.LedRoteadorMensagensMInsBDadosInativo.Visibility = Visibility.Hidden;
                    form.LedRoteadorMensagensMsgRotInativo.Visibility = Visibility.Hidden;
                    form.LedRoteadorPrivateMemoryInativo.Visibility = Visibility.Hidden;
                    form.LedRoteadorThreadCountInativo.Visibility = Visibility.Hidden;
                    form.LedRoteadorMensagensFilaInterna3Inativo.Visibility = Visibility.Hidden;
                    form.LedFilaRoteadorFilaAcessivelInativo.Visibility = Visibility.Hidden;
                    form.LedFilaRoteadorMsgRepresadasInativo.Visibility = Visibility.Hidden;
                    form.LedFilaRoteadorTamFilaInativo.Visibility = Visibility.Hidden;


                }
                else
                {
                    form.LedRoteadorHandleCountInativo.Visibility = Visibility.Visible;
                    form.LedRoteadorMensagensFilaGDadosInativo.Visibility = Visibility.Hidden;
                    form.LedRoteadorMensagensFilaInterna2Inativo.Visibility = Visibility.Visible;
                    form.LedRoteadorMensagensMInsBDadosInativo.Visibility = Visibility.Visible;
                    form.LedRoteadorMensagensMsgRotInativo.Visibility = Visibility.Visible;
                    form.LedRoteadorPrivateMemoryInativo.Visibility = Visibility.Visible;
                    form.LedRoteadorThreadCountInativo.Visibility = Visibility.Visible;
                    form.LedRoteadorMensagensFilaInterna3Inativo.Visibility = Visibility.Visible;
                    form.LedFilaRoteadorFilaAcessivelInativo.Visibility = Visibility.Visible;
                    form.LedFilaRoteadorMsgRepresadasInativo.Visibility = Visibility.Visible;
                    form.LedFilaRoteadorTamFilaInativo.Visibility = Visibility.Visible;
                }
            }
        }

        private void SetReceptorState(STMMonitor form, bool active)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetEnable d = new dSetEnable(SetReceptorState);
                this.Dispatcher.Invoke(d, new object[] { form, active });
            }
            else
            {
                form.Receptor.IsEnabled = active;
                if (active)
                {
                    form.LedReceptorFilasFilaInternaInativo.Visibility = Visibility.Hidden;
                    form.LedReceptorFilasLimiteInativo.Visibility = Visibility.Hidden;
                    form.LedReceptorHandleCountInativo.Visibility = Visibility.Hidden;
                    form.LedReceptorPrivateMemoryInativo.Visibility = Visibility.Hidden;
                    form.LedReceptorThreadCountInativo.Visibility = Visibility.Hidden;
                    form.LedSTMConexaoInativo.Visibility = Visibility.Hidden;
                    form.LedSTMTMedioInativo.Visibility = Visibility.Hidden;
                }
                else
                {
                    form.LedReceptorFilasFilaInternaInativo.Visibility = Visibility.Visible;
                    form.LedReceptorFilasLimiteInativo.Visibility = Visibility.Visible;
                    form.LedReceptorHandleCountInativo.Visibility = Visibility.Visible;
                    form.LedReceptorPrivateMemoryInativo.Visibility = Visibility.Visible;
                    form.LedReceptorThreadCountInativo.Visibility = Visibility.Visible;
                    form.LedSTMConexaoInativo.Visibility = Visibility.Visible;
                    form.LedSTMTMedioInativo.Visibility = Visibility.Visible;
                }
            }
        }

        private void SetDumperState(STMMonitor form, bool active)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetEnable d = new dSetEnable(SetDumperState);
                this.Dispatcher.Invoke(d, new object[] { form, active });
            }
            else
            {
                form.Dumper.IsEnabled = active;
                form.Fila_Dumper.IsEnabled = active;

                if (active)
                {
                    form.LedFilaDumperFilaAcessivelInoperante.Visibility = Visibility.Hidden;
                    form.LedFilaDumperMsgRepresadasInativo.Visibility = Visibility.Hidden;
                    form.LedFilaDumperTamFilaInativo.Visibility = Visibility.Hidden;
                    form.LedDumperHandleCountInativo.Visibility = Visibility.Hidden;
                }
                else
                {
                    form.LedFilaDumperFilaAcessivelInoperante.Visibility = Visibility.Visible;
                    form.LedFilaDumperMsgRepresadasInativo.Visibility = Visibility.Visible;
                    form.LedFilaDumperTamFilaInativo.Visibility = Visibility.Visible;
                    form.LedDumperHandleCountInativo.Visibility = Visibility.Visible;
                }
            }
        }

        private void SetOrderExecutorState(STMMonitor form, bool active)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetEnable d = new dSetEnable(SetOrderExecutorState);
                this.Dispatcher.Invoke(d, new object[] { form, active });
            }
            else
            {
                form.ExecutorOrdens.IsEnabled = active;
                if (active)
                {
                    form.LedExecutorOrdensTempoProcInativo.Visibility = Visibility.Hidden;
                    form.LedExecutorFinanceiroTempoProcInativo.Visibility = Visibility.Hidden;
                    form.LedExecutorCasamentoTempoProcInativo.Visibility = Visibility.Hidden;
                    form.LedExecutorNegociosTempoProcInativo.Visibility = Visibility.Hidden;
                }
                else
                {
                    form.LedExecutorOrdensTempoProcInativo.Visibility = Visibility.Visible;
                    form.LedExecutorFinanceiroTempoProcInativo.Visibility = Visibility.Visible;
                    form.LedExecutorCasamentoTempoProcInativo.Visibility = Visibility.Visible;
                    form.LedExecutorNegociosTempoProcInativo.Visibility = Visibility.Visible;

                    form.LedExecutorOrdensTempoProcOK.Visibility = Visibility.Hidden;
                    form.LedExecutorFinanceiroTempoProcOK.Visibility = Visibility.Hidden;
                    form.LedExecutorCasamentoTempoProcOK.Visibility = Visibility.Hidden;
                    form.LedExecutorNegocioTempoProcOK.Visibility = Visibility.Hidden;
                    form.LedExecutorOrdensTempoProcNOK.Visibility = Visibility.Hidden;
                    form.LedExecutorFinanceiroTempoProcNOK.Visibility = Visibility.Hidden;
                    form.LedExecutorCasamentoTempoProcNOK.Visibility = Visibility.Hidden;
                    form.LedExecutorNegocioTempoProcNOK.Visibility = Visibility.Hidden;
                }
            }
        }

        private void SetHFTProcessState(STMMonitor form, bool active)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetEnable d = new dSetEnable(SetHFTProcessState);
                this.Dispatcher.Invoke(d, new object[] { form, active });
            }
            else
            {
                form.ProcessosHFT.IsEnabled = active;
                if (active)
                {
                    if (!_hftFora)
                    {
                        form.AlertaHFT01Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT02Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT03Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT04Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT05Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT06Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT07Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT08Inativo.Visibility = Visibility.Hidden;
                    }
                    form.AlertaHFTServersInativo.Visibility = Visibility.Hidden;
                }
                else
                {
                    form.AlertaHFTServersInativo.Visibility = Visibility.Visible;
                    if (!_hftFora)
                    {
                        form.AlertaHFT01Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT02Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT03Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT04Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT05Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT06Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT07Inativo.Visibility = Visibility.Hidden;
                        form.AlertaHFT08Inativo.Visibility = Visibility.Hidden;
                    }
                    //form.AlertaHFTServersInativo.Visibility = Visibility.Hidden;
                }
            }
        }

        private void FillToolTipClient()
        {
            if (_presenter.ItemHFTProcessMonitor != null)
            {
                if (this.PanelClientes.Children.Count > 0)
                    this.PanelClientes.Children.Clear();
                foreach (Int32? user in _presenter.ItemHFTProcessMonitor.ClientRule)
                {
                    TextBlock client = new TextBlock();
                    if (client != null && user.HasValue)
                        client.Text = user.Value.ToString();
                    else
                        client.Text = "N/A";

                    this.PanelClientes.Children.Add(client);
                }
                if (this.PanelClientes.Children.Count == 0)
                {
                    TextBlock client = new TextBlock();
                    client.Text = "N/A";
                    this.PanelClientes.Children.Add(client);
                }
            }
        }

        private void FillToolTipPorts()
        {
            if (_presenter.ItemHFTProcessMonitor != null)
            {
                if (this.PanelPortas.Children.Count > 0)
                    this.PanelPortas.Children.Clear();
                foreach (String ports in _presenter.ItemHFTProcessMonitor.PortRange)
                {
                    TextBlock client = new TextBlock();
                    if (client != null && !String.IsNullOrEmpty(ports))
                        client.Text = ports;
                    else
                        client.Text = "N/A";

                    this.PanelPortas.Children.Add(client);
                }
                if (this.PanelPortas.Children.Count == 0)
                {
                    TextBlock client = new TextBlock();
                    client.Text = "N/A";
                    this.PanelPortas.Children.Add(client);
                }
            }
        }

        public void CheckErrorList()
        {
            if (_presenter.NumberOfHftProcessActived != _presenter.entityMonitor.NumberOfHftProcess)
            {
                _hftFora = true;
                CheckHFTProcess(this);
            }
            else
            {
                _hftFora = false;
            }
            if (_presenter.ContainsErrors)
            {
                SetError(this);
            }
            else
            {
                ClearError(this);

            }
        }

        private void CheckHFTProcess(STMMonitor form)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetError d = new dSetError(CheckHFTProcess);
                this.Dispatcher.Invoke(d, new object[] { form });
            }
            else
            {
                SetHFTProcessState(this, true);
                if (form._presenter.entityMonitor.NumberOfHftProcess > form._presenter.NumberOfHftProcessActived)
                {
                    form.AlertaHFTServersNOK.ToolTip = "Servidor HFT fora do ar";
                    form.AlertaHFTServersNOK.Visibility = Visibility.Visible;
                }
                else if (form._presenter.entityMonitor.NumberOfHftProcess < form._presenter.NumberOfHftProcessActived)
                {
                    form.AlertaHFTServersNOK.ToolTip = "Existe servidores HFT a mais do que foi especificado no config, por favor verifique.";
                    form.AlertaHFTServersNOK.Visibility = Visibility.Visible;
                }

                Int32 hftProcess = 1;
                if (!_presenter.CheckHftProcessOk(_presenter.HFTProcessList[hftProcess-1]) && (form._presenter.entityMonitor.NumberOfHftProcess >= hftProcess || form._presenter.NumberOfHftProcessActived >= hftProcess))
                {
                    form.HFTProcess01.Visibility = Visibility.Visible;
                    form.AlertaHFT01Inativo.Visibility = Visibility.Visible;
                }
                else
                {
                    form.AlertaHFT01Inativo.Visibility = Visibility.Hidden;
                }
                hftProcess++;

                if (!_presenter.CheckHftProcessOk(_presenter.HFTProcessList[hftProcess - 1]) && (form._presenter.entityMonitor.NumberOfHftProcess >= hftProcess || form._presenter.NumberOfHftProcessActived >= hftProcess))
                {
                    form.HFTProcess02.Visibility = Visibility.Visible;
                    form.AlertaHFT02Inativo.Visibility = Visibility.Visible;
                }
                else
                {
                    form.AlertaHFT02Inativo.Visibility = Visibility.Hidden;
                }
                hftProcess++;
                if (!_presenter.CheckHftProcessOk(_presenter.HFTProcessList[hftProcess - 1]) && (form._presenter.entityMonitor.NumberOfHftProcess >= hftProcess || form._presenter.NumberOfHftProcessActived >= hftProcess))
                {
                    form.HFTProcess03.Visibility = Visibility.Visible;
                    form.AlertaHFT03Inativo.Visibility = Visibility.Visible;
                }
                else
                {
                    form.AlertaHFT03Inativo.Visibility = Visibility.Hidden;
                }
                hftProcess++;
                if (!_presenter.CheckHftProcessOk(_presenter.HFTProcessList[hftProcess - 1]) && (form._presenter.entityMonitor.NumberOfHftProcess >= hftProcess || form._presenter.NumberOfHftProcessActived >= hftProcess))
                {
                    form.HFTProcess04.Visibility = Visibility.Visible;
                    form.AlertaHFT04Inativo.Visibility = Visibility.Visible;
                }
                else
                {
                    form.AlertaHFT04Inativo.Visibility = Visibility.Hidden;
                }
                hftProcess++;
                if (!_presenter.CheckHftProcessOk(_presenter.HFTProcessList[hftProcess - 1]) && (form._presenter.entityMonitor.NumberOfHftProcess >= hftProcess || form._presenter.NumberOfHftProcessActived >= hftProcess))
                {
                    form.HFTProcess05.Visibility = Visibility.Visible;
                    form.AlertaHFT05Inativo.Visibility = Visibility.Visible;
                }
                else
                {
                    form.AlertaHFT05Inativo.Visibility = Visibility.Hidden;
                }
                hftProcess++;
                if (!_presenter.CheckHftProcessOk(_presenter.HFTProcessList[hftProcess - 1]) && (form._presenter.entityMonitor.NumberOfHftProcess >= hftProcess || form._presenter.NumberOfHftProcessActived >= hftProcess))
                {
                    form.HFTProcess06.Visibility = Visibility.Visible;
                    form.AlertaHFT06Inativo.Visibility = Visibility.Visible;
                }
                else
                {
                    form.AlertaHFT06Inativo.Visibility = Visibility.Hidden;
                }
                hftProcess++;
                if (!_presenter.CheckHftProcessOk(_presenter.HFTProcessList[hftProcess - 1]) && (form._presenter.entityMonitor.NumberOfHftProcess >= hftProcess || form._presenter.NumberOfHftProcessActived >= hftProcess))
                {
                    form.HFTProcess07.Visibility = Visibility.Visible;
                    form.AlertaHFT07Inativo.Visibility = Visibility.Visible;
                }
                else
                {
                    form.AlertaHFT07Inativo.Visibility = Visibility.Hidden;
                }
                hftProcess++;
                if (!_presenter.CheckHftProcessOk(_presenter.HFTProcessList[hftProcess - 1]) && (form._presenter.entityMonitor.NumberOfHftProcess >= hftProcess || form._presenter.NumberOfHftProcessActived >= hftProcess))
                {
                    form.HFTProcess08.Visibility = Visibility.Visible;
                    form.AlertaHFT08Inativo.Visibility = Visibility.Visible;
                }
                else
                {
                    form.AlertaHFT08Inativo.Visibility = Visibility.Hidden;
                }
            }
        }

        private void ClearError(STMMonitor form)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetError d = new dSetError(ClearError);
                this.Dispatcher.Invoke(d, new object[] { form });
            }
            else
            {
                this.LinhaErro1.Content = "";
                this.LinhaErro2.Content = "";
                this.LinhaErro3.Content = "";
                this.LinhaErro4.Content = "";
                this.LinhaErro5.Content = "";
                Storyboard HideErrorList = (Storyboard)Resources["AvisoSai"];
                HideErrorList.Begin();
                _eventoOcorreu = false;
            }
        }

        private void ClearLinesErrors(STMMonitor form)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetError d = new dSetError(ClearLinesErrors);
                this.Dispatcher.Invoke(d, new object[] { form });
            }
            else
            {
                this.LinhaErro1.Content = "";
                this.LinhaErro2.Content = "";
                this.LinhaErro3.Content = "";
                this.LinhaErro4.Content = "";
                this.LinhaErro5.Content = "";
            }
        }

        private void AddError()
        {
            ClearLinesErrors(this);
            int linha = 0;
            if (_presenter.CheckErrors(_presenter.entityMonitor.DumperMonitor.ErrorData))
            {
                this.LinhaErro1.Content = "Dumper";
                this.LinhaErro1.ToolTip = _presenter.entityMonitor.DumperMonitor.ErrorData.Source + " " + _presenter.entityMonitor.DumperMonitor.ErrorData.ExceptionMessage;
                linha = 1;
            }
            if (_presenter.CheckErrors(_presenter.entityMonitor.OrderExecutor.ErrorData))
            {
                if (linha == 1)
                {
                    this.LinhaErro2.Content = "Executor";
                    this.LinhaErro2.ToolTip = _presenter.entityMonitor.OrderExecutor.ErrorData.Source + " " + _presenter.entityMonitor.OrderExecutor.ErrorData.ExceptionMessage;
                    linha = 2;
                }
                else
                {
                    this.LinhaErro1.Content = "Executor";
                    this.LinhaErro1.ToolTip = _presenter.entityMonitor.OrderExecutor.ErrorData.Source + " " + _presenter.entityMonitor.OrderExecutor.ErrorData.ExceptionMessage;
                    linha = 1;
                }
            }
            else if (_presenter.CheckErrors(_presenter.entityMonitor.MatchExecutor.ErrorData))
            {
                if (linha == 1)
                {
                    this.LinhaErro2.Content = "Executor";
                    this.LinhaErro2.ToolTip = _presenter.entityMonitor.MatchExecutor.ErrorData.Source + " " + _presenter.entityMonitor.MatchExecutor.ErrorData.ExceptionMessage;
                    linha = 2;
                }
                else
                {
                    this.LinhaErro1.Content = "Executor";
                    this.LinhaErro1.ToolTip = _presenter.entityMonitor.MatchExecutor.ErrorData.Source + " " + _presenter.entityMonitor.MatchExecutor.ErrorData.ExceptionMessage;
                    linha = 1;
                }
            }
            else if (_presenter.CheckErrors(_presenter.entityMonitor.FinancialExecutor.ErrorData))
            {
                if (linha == 1)
                {
                    this.LinhaErro2.Content = "Executor";
                    this.LinhaErro2.ToolTip = _presenter.entityMonitor.FinancialExecutor.ErrorData.Source + " " + _presenter.entityMonitor.FinancialExecutor.ErrorData.ExceptionMessage;
                    linha = 2;
                }
                else
                {
                    this.LinhaErro1.Content = "Executor";
                    this.LinhaErro1.ToolTip = _presenter.entityMonitor.FinancialExecutor.ErrorData.Source + " " + _presenter.entityMonitor.FinancialExecutor.ErrorData.ExceptionMessage;
                    linha = 1;
                }
            }
            else if (_presenter.CheckErrors(_presenter.entityMonitor.BusinessExecutor.ErrorData))
            {
                if (linha == 1)
                {
                    this.LinhaErro2.Content = "Executor";
                    this.LinhaErro2.ToolTip = _presenter.entityMonitor.BusinessExecutor.ErrorData.Source + " " + _presenter.entityMonitor.BusinessExecutor.ErrorData.ExceptionMessage;
                    linha = 2;
                }
                else
                {
                    this.LinhaErro1.Content = "Executor";
                    this.LinhaErro1.ToolTip = _presenter.entityMonitor.BusinessExecutor.ErrorData.Source + " " + _presenter.entityMonitor.BusinessExecutor.ErrorData.ExceptionMessage;
                    linha = 1;
                }
            }
            if (_presenter.CheckErrors(_presenter.entityMonitor.ReceptorMonitor.ErrorData))
            {
                if (linha == 2)
                {
                    this.LinhaErro3.Content = "Receptor";
                    this.LinhaErro3.ToolTip = _presenter.entityMonitor.ReceptorMonitor.ErrorData.Source + " " + _presenter.entityMonitor.ReceptorMonitor.ErrorData.ExceptionMessage;
                    linha = 3;
                }
                else if (linha == 1)
                {
                    this.LinhaErro2.Content = "Receptor";
                    this.LinhaErro2.ToolTip = _presenter.entityMonitor.ReceptorMonitor.ErrorData.Source + " " + _presenter.entityMonitor.ReceptorMonitor.ErrorData.ExceptionMessage;
                    linha = 2;
                }
                else
                {
                    this.LinhaErro1.Content = "Receptor";
                    this.LinhaErro1.ToolTip = _presenter.entityMonitor.ReceptorMonitor.ErrorData.Source + " " + _presenter.entityMonitor.ReceptorMonitor.ErrorData.ExceptionMessage;
                    linha = 1;
                }
            }
            if (_presenter.CheckErrors(_presenter.entityMonitor.RouterMonitor.ErrorData))
            {
                if (linha == 3)
                {
                    this.LinhaErro4.Content = "Router";
                    this.LinhaErro4.ToolTip = _presenter.entityMonitor.RouterMonitor.ErrorData.Source + " " + _presenter.entityMonitor.RouterMonitor.ErrorData.ExceptionMessage;
                    linha = 4;
                }
                if (linha == 2)
                {
                    this.LinhaErro3.Content = "Router";
                    this.LinhaErro3.ToolTip = _presenter.entityMonitor.RouterMonitor.ErrorData.Source + " " + _presenter.entityMonitor.RouterMonitor.ErrorData.ExceptionMessage;
                    linha = 3;
                }
                else if (linha == 1)
                {
                    this.LinhaErro2.Content = "Router";
                    this.LinhaErro2.ToolTip = _presenter.entityMonitor.RouterMonitor.ErrorData.Source + " " + _presenter.entityMonitor.RouterMonitor.ErrorData.ExceptionMessage;
                    linha = 2;
                }
                else
                {
                    this.LinhaErro1.Content = "Router";
                    this.LinhaErro1.ToolTip = _presenter.entityMonitor.RouterMonitor.ErrorData.Source + " " + _presenter.entityMonitor.RouterMonitor.ErrorData.ExceptionMessage;
                    linha = 1;
                }
            }
            if (_presenter.HftErrors())
            {
                StringBuilder sb = new StringBuilder();
                foreach (HFTProcessMonitorEntity hft in _presenter.entityMonitor.HFTMonitors)
                {
                    if (hft != null && hft.ErrorData != null)
                    {
                        sb.Append(hft.ErrorData.Source + " " + hft.ErrorData.ExceptionMessage + " ");
                    }
                }
                if (linha == 4)
                {
                    this.LinhaErro5.Content = "HFT Process";
                    this.LinhaErro5.ToolTip = sb.ToString();
                    linha = 5;
                }
                if (linha == 3)
                {
                    this.LinhaErro4.Content = "HFT Process";
                    this.LinhaErro4.ToolTip = sb.ToString();
                    linha = 4;
                }
                if (linha == 2)
                {
                    this.LinhaErro3.Content = "HFT Process";
                    this.LinhaErro3.ToolTip = sb.ToString();
                    linha = 3;
                }
                else if (linha == 1)
                {
                    this.LinhaErro2.Content = "HFT Process";
                    this.LinhaErro2.ToolTip = sb.ToString();
                    linha = 2;
                }
                else
                {
                    this.LinhaErro1.Content = "HFT Process";
                    this.LinhaErro1.ToolTip = sb.ToString();
                    linha = 1;
                }
            }
        }

        private void SetError(STMMonitor form)
        {
            if (this.Geral.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                dSetError d = new dSetError(SetError);
                this.Dispatcher.Invoke(d, new object[] { form });
            }
            else
            {
                AddError();
                if (!_eventoOcorreu)
                {
                    Storyboard ShowErrorList = (Storyboard)Resources["AvisoEntra"];
                    ShowErrorList.Begin();
                    _eventoOcorreu = true;
                }
            }
        }

        public void KeepAlive()
        {
            WPFMessageManager _wpfMessageManager = new WPFMessageManager();
            InternalMessage im = new InternalMessage();
            im.MessageId = MessageIds.WindowManager.SNC_WINDOW_ACTIVITY;
            _wpfMessageManager.SendMessage(im);

        }
    }

}

